package com.mcx.modules.proto;

import com.mcx.modules.common.IProto;
import com.mcx.modules.common.MMO_DataStream;

import java.io.*;
import java.util.*;

/**
 * 客户端发送战斗胜利消息
 */
public class GameLevel_VictoryProto implements IProto
{
    private static MMO_DataStream dataStream = new MMO_DataStream();

    @Override
    public short getProtoCode() { return 12003; }

    public Integer GameLevelId; //游戏关卡Id
    public byte Grade; //难度等级
    public byte Star; //获得星级
    public Integer Exp; //获得经验
    public Integer Gold; //获得金币
    public Integer KillTotalMonsterCount; //杀怪数量
    public ArrayList<MonsterItem> KillMonsterList; //杀怪列表
    public Integer GoodsTotalCount; //获得物品数量
    public ArrayList<GoodsItem> GetGoodsList; //获得物品

    /**
     * 杀怪列表
     */
    public static class MonsterItem
    {
        public Integer MonsterId; //怪Id
        public Integer MonsterCount; //怪数量
    }

    /**
     * 获得物品
     */
    public static class GoodsItem
    {
        public byte GoodsType; //物品类型
        public Integer GoodsId; //物品Id
        public Integer GoodsCount; //物品数量
    }

    public byte[] ToArray() {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try (DataOutputStream outputStream = new DataOutputStream(out)) {
            dataStream.outputStream = outputStream;
            dataStream.WriteShort(getProtoCode());
            dataStream.WriteInt(GameLevelId);
            dataStream.WriteByte(Grade);
            dataStream.WriteByte(Star);
            dataStream.WriteInt(Exp);
            dataStream.WriteInt(Gold);
            dataStream.WriteInt(KillTotalMonsterCount);
            for (int i = 0; i < KillTotalMonsterCount; i++) {
                dataStream.WriteInt(KillMonsterList.get(i).MonsterId);
                dataStream.WriteInt(KillMonsterList.get(i).MonsterCount);
            }
            dataStream.WriteInt(GoodsTotalCount);
            for (int i = 0; i < GoodsTotalCount; i++) {
                dataStream.WriteByte(GetGoodsList.get(i).GoodsType);
                dataStream.WriteInt(GetGoodsList.get(i).GoodsId);
                dataStream.WriteInt(GetGoodsList.get(i).GoodsCount);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return out.toByteArray();    }
    public static GameLevel_VictoryProto GetProto(byte[] buffer) {
        GameLevel_VictoryProto proto = new GameLevel_VictoryProto();
        try (DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(buffer))) {
            dataStream.inputStream = inputStream;
            proto.GameLevelId = dataStream.ReadInt();
            proto.Grade = (byte)dataStream.ReadByte();
            proto.Star = (byte)dataStream.ReadByte();
            proto.Exp = dataStream.ReadInt();
            proto.Gold = dataStream.ReadInt();
            proto.KillTotalMonsterCount = dataStream.ReadInt();
            proto.KillMonsterList = new ArrayList<MonsterItem>();
            for (int i = 0; i < proto.KillTotalMonsterCount; i++) {
                MonsterItem _KillMonster = new MonsterItem();
                _KillMonster.MonsterId = dataStream.ReadInt();
                _KillMonster.MonsterCount = dataStream.ReadInt();
                proto.KillMonsterList.add(_KillMonster);
            }
            proto.GoodsTotalCount = dataStream.ReadInt();
            proto.GetGoodsList = new ArrayList<GoodsItem>();
            for (int i = 0; i < proto.GoodsTotalCount; i++) {
                GoodsItem _GetGoods = new GoodsItem();
                _GetGoods.GoodsType = (byte)dataStream.ReadByte();
                _GetGoods.GoodsId = dataStream.ReadInt();
                _GetGoods.GoodsCount = dataStream.ReadInt();
                proto.GetGoodsList.add(_GetGoods);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return proto;
    }
}